import { computed, ref } from 'vue'
import { router } from '@/router/index'
import { useAppStore } from '@/store/app'

export const active = ref(false)
export const ZINDEX = ref(0)
export const FatherID = ref(null)
export const popovervisible = ref(false)



export const MobileLsit = computed(() => {
  const userStore = useAppStore()

  let result = { children: userStore.headerList }
  foreachTree(userStore.headerList, (item) => {
    if (item.id === FatherID.value)
      result = item
  })
  return result
})

export const tapBack = () => {
  if (ZINDEX.value === 0) {
    active.value = false
  }
  else {
    ZINDEX.value -= 1
    FatherID.value = MobileLsit.value.fatherId
  }
}
//
export const tapList = (e) => {
  const userStore = useAppStore()

  if (e.hasOwnProperty('children')) {
    ZINDEX.value = e.children[0].zIndex
    FatherID.value = e.id
  }
  else {
    if (ZINDEX.value === 0) {
      router.push(e.path)
    }
    else {
      let fatherItem
      foreachTree(userStore.headerList, (item) => {
        if (item.id === e.fatherId)
          fatherItem = item
      })

      if (fatherItem.popUpType == 'hover')
        fatherItem.pageIndex = e.pageIndex

      router.push(`${e.path}?tit=${MobileLsit.value.title}&opt=${encodeURIComponent(JSON.stringify(e))}`)
    }

    active.value = false
  }
}

export const afterLeave = () => {
  ZINDEX.value = 0
  FatherID.value = null
}

// pc
export const isShowCT = ref(false)

export const isFocus = ref(false)

export const onBlurFn = () => {
  isFocus.value = false
}

export const nowID = ref(null)

export const openDrawer = (t, status = 'detail') => {

  const userStore = useAppStore()
  if (userStore.pattern === 'Mobile') {
    // 处理手机端
    if (status === 'applicatioCenter') {
      // 点击的二级面包屑
      let fatherItem
      foreachTree(userStore.headerList, (item) => {
        if (item.id === t.fatherId)
          fatherItem = item
      })
      FatherID.value = fatherItem.fatherId
      ZINDEX.value = fatherItem.zIndex
    }
    else {
      FatherID.value = t.fatherId
      ZINDEX.value = t.zIndex
    }
    active.value = true
  }
  else {
    const opt = router?.currentRoute.value?.query?.opt
    // console.log(opt, 'optopt')
    if (opt)
      nowID.value = JSON.parse(decodeURIComponent(opt)).id
    // if (t)
    //   nowID.value = t.id
    else
      nowID.value = null

    // 处理pc和pad端
    isShowCT.value = true
    // 高亮处理
    // if (t)
    //   nowID.value = t.id
  }
}

export const tapItems = (e) => {
  console.log(e, '11')
  if (isShowCT.value)
    isShowCT.value = false
  else
    openDrawer(e)
}

export const pcTabsList = ref([])
export const HList = ref(-1)

export const closeTanK = () => {
  HList.value = -1
  pcTabsList.value = []
  isShowCT.value = false
}

export const tapA = (e, v, index) => {
  console.log(isShowCT.value, 'isShowCT')
  if (e.path) {
    closeTanK()
    return router.push(e.path)
  }

  if (e.children && e.children.length > 0) {
    if (isShowCT.value) {
      return closeTanK()
    } else {
      HList.value = index
      pcTabsList.value = e.children
      return openDrawer(v)
    }
    if (HList.value === index) {
      console.log('HList')
      return closeTanK()
    }
    else {
      console.log('pcTabsList')
      HList.value = index
      pcTabsList.value = e.children
      openDrawer(v)
    }
  }
}

export const onfocusFn = () => {
  closeTanK()
  isFocus.value = true
}
